package com.example.lib5.binaryTree.test;

import javax.management.relation.Role;

/**
 * 创建日期：2022/5/27 0027 17:08
 *
 * @author tony.sun
 * 类说明：
 */
public class BinaryDemoB {
    public static void main(String[] args) {
        //二叉树
        Binary binary=new Binary();
        RoleNote root=new RoleNote(1,"唐三");
        RoleNote role2=new RoleNote(2,"千仞雪");
        RoleNote role3=new RoleNote(3,"小舞");
        RoleNote role4=new RoleNote(4,"比比东");
        RoleNote role5=new RoleNote(5,"小舞");
        root.setLeft(role2);
        root.setRight(role3);
        role3.setRight(role4);
        role3.setLeft(role5);
        binary.setRoot(root);
        System.out.println("遍历---------------");
        binary.preOrder();
        System.out.println("遍历---------------");
        binary.infixOrder();
        System.out.println("遍历---------------");
        binary.postOrder();

        //前中后查询
        RoleNote roleNote = binary.preSearch(5);
        if (roleNote!=null) {
            System.out.println(roleNote);
        }else {
            System.out.println("没有找到");
        }
        RoleNote roleNote1 = binary.infixSearch(5);
        if (roleNote!=null) {
            System.out.println(roleNote1);
        }else {
            System.out.println("没有找到");
        }
        RoleNote roleNote2 = binary.postSearch(5);
        if (roleNote!=null) {
            System.out.println(roleNote2);
        }else {
            System.out.println("没有找到");
        }

        binary.delete(5);
        System.out.println("删除后的节点遍历");
        binary.preOrder();

    }
}
//节点
class RoleNote{
    private int no;
    private String name;
    private RoleNote left;
    private RoleNote right;

    public RoleNote(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "RoleNote{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void setLeft(RoleNote role) {
        left=role;
    }

    public void setRight(RoleNote role) {
        right=role;
    }

    //前中后序遍历
    public void preOrder() {
        System.out.println(this);
        if (this.left!=null){
            this.left.preOrder();
        }
        if (this.right!=null){
            this.right.preOrder();
        }
    }
    public void infixOrder() {
        if (this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    public void postOrder() {
        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public RoleNote preSearch(int no) {
        System.out.println("哈哈哈~~");
        if (this.no==no) {
            return this;
        }
        RoleNote findRoleNote=null;
        if (this.left!=null) {
            findRoleNote=this.left.preSearch(no);
        }
        if (findRoleNote!=null) {
            return findRoleNote;
        }
        if (this.right!=null) {
            findRoleNote=this.right.preSearch(no);
        }
        return findRoleNote;

    }
    public RoleNote infixSearch(int no) {

        RoleNote findRoleNote=null;
        if (this.left!=null) {
            findRoleNote=this.left.infixSearch(no);
        }
        if (findRoleNote!=null) {
            return findRoleNote;
        }
        System.out.println("哈哈哈~~");
        if (this.no==no) {
            return this;
        }
        if (this.right!=null) {
            findRoleNote=this.right.infixSearch(no);
        }
        return findRoleNote;

    }
    public RoleNote postSearch(int no) {

        RoleNote findRoleNote=null;
        if (this.left!=null) {
            findRoleNote=this.left.postSearch(no);
        }
        if (findRoleNote!=null) {
            return findRoleNote;
        }

        if (this.right!=null) {
            findRoleNote=this.right.postSearch(no);
        }
        if (findRoleNote!=null) {
            return findRoleNote;
        }
        System.out.println("哈哈哈~~");
        if (this.no==no) {
            return this;
        }
        return findRoleNote;

    }

    public void delete(int no) {
        if (this.left!=null&&this.left.no==no) {
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no) {
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delete(no);
        }
        if (this.right!=null){
            this.right.delete(no);
        }
    }
}

//二叉树
class Binary{
    //头结点
    RoleNote root;
    public void setRoot(RoleNote root) {
        this.root=root;
    }
    //前序遍历
    public void preOrder(){
        if (root!=null) {
            root.preOrder();
        }else {
            System.out.println("链表为空");
        }
    }
    //中序遍历
    public void infixOrder(){
        if (root!=null) {
            root.infixOrder();
        }else {
            System.out.println("链表为空");
        }
    }
    //后序遍历
    public void postOrder(){
        if (root!=null) {
            root.postOrder();
        }else {
            System.out.println("链表为空");
        }
    }

    public RoleNote preSearch(int no) {
        if (root!=null) {
            return root.preSearch(no);
        }else {
            return null;
        }
    }
    public RoleNote infixSearch(int no) {
        if (root!=null) {
            return root.infixSearch(no);
        }else {
            return null;
        }
    }
    public RoleNote postSearch(int no) {
        if (root!=null) {
            return root.postSearch(no);
        }else {
            return null;
        }
    }


    public void delete(int no) {
        if (root!=null) {
            if (root.getNo()==no) {
                root=null;
            }else {
                root.delete(no);
            }
        }else {
            System.out.println("二叉树为空");
        }
    }
}
